/**
 * Project Name:zyhj-web
 * File Name:YjfkCorsServiceImpl.java
 * Package Name:com.vviton.zyhj.consumer.provider
 * Date:2017年3月6日下午2:52:31
 *
 */

package com.vviton.zyhj.consumer.provider;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.vviton.zyhj.common.base.AppConfig;
import com.vviton.zyhj.common.base.BaseService;
import com.vviton.zyhj.common.common.CodeConstants;
import com.vviton.zyhj.common.common.GlobalConstants;
import com.vviton.zyhj.common.dao.TYyxtO2oYjfkxxMapper;
import com.vviton.zyhj.common.entity.TYyxtCode;
import com.vviton.zyhj.common.entity.TYyxtO2oYjfkxx;
import com.vviton.zyhj.common.utils.DateUtils;
import com.vviton.zyhj.common.utils.FileBean;
import com.vviton.zyhj.common.utils.ResultJson;
import com.vviton.zyhj.common.view.ParamYjfk;
import com.vviton.zyhj.common.view.ViewYjfkxx;

/**
 * ClassName:YjfkCorsServiceImpl <br/>
 * Function: 意见反馈对外接口实现类. <br/>
 * Date: 2017年3月6日 下午2:52:31 <br/>
 *
 * @author libo
 * @version
 * @since JDK 1.7
 * @see
 */
public class YjfkCorsServiceImpl extends BaseService implements YjfkCorsService {

    /** appConfig */
    @Autowired
    private AppConfig appConfig;

    /** o2o意见反馈DAO */
    @Autowired
    private TYyxtO2oYjfkxxMapper tYyxtO2oYjfkxxMapper;

    /**
     *
     * insertYjfkxx,(新录入意见反馈信息). <br/>
     * Author: libo <br/>
     * Create Date: 2017年3月6日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2017年3月6日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param record 意见反馈信息对象
     * @return ResultJson
     * @since JDK 1.7
     */
    @Override
    public ResultJson insertYjfkxx(ParamYjfk yjfk) {
        ResultJson result = new ResultJson(false);

        if (StringUtils.isNotBlank(yjfk.getTcrzh())) {
            TYyxtO2oYjfkxx record = new TYyxtO2oYjfkxx();

            record.setYwy(yjfk.getYwy());
            record.setTcrzh(yjfk.getTcrzh());
            record.setTcrmc(yjfk.getTcrmc());
            record.setTcrjg(yjfk.getTcrjg());
            record.setBt(yjfk.getBt());
            record.setWtlx(yjfk.getWtlx());
            //保存UE富文本附件内容到服务器，并返回此附件路径
            if (StringUtils.isNotBlank(yjfk.getWtnr())) {
                ResultJson resultJson = this.saveTxtToDFS(appConfig.getDfsUploadUrl(), yjfk.getWtnr());
                if (resultJson.isResult()) {
                    FileBean fb = (FileBean) resultJson.getObject();
                    if (fb != null) {
                        record.setWtnr(fb.getFilePath());
                    }
                }
            }

            record.setId(this.getKey());
            record.setWtzt(CodeConstants.WTZT_DCL);
            record.setTcsj(new Date());
            record.setDelflag(GlobalConstants.DELFLAG_ADD);
            record.setVersions(this.getVersion());
            record.setOperatercode(record.getTcrzh());
            record.setOperatetime(record.getTcsj());
            record.setCreatercode(record.getTcrzh());
            record.setCreatertime(record.getTcsj());
            int count = tYyxtO2oYjfkxxMapper.insertYjfkxx(record);
            if (count == 1) {
                result.setResult(true);
            }
        }

        return result;
    }

    /**
     *
     * getYjfkxxByZh,(拉取该账号下意见数据列表). <br/>
     * Author: libo <br/>
     * Create Date: 2017年3月6日 <br/>
     * ===============================================================<br/>
     * Modifier: libo <br/>
     * Modify Date: 2017年3月6日 <br/>
     * Modify Description: <br/>
     * ===============================================================<br/>
     *
     * @param tcrzh 提出人账号
     * @param yhtx 用户头像
     * @return List<ViewYjfkxx>
     * @since JDK 1.7
     */
    @Override
    public List<ViewYjfkxx> getYjfkxxByZh(String tcrzh, String yhtx) {
        if (StringUtils.isNotBlank(tcrzh)) {
            List<ViewYjfkxx> yjfkList = tYyxtO2oYjfkxxMapper.getYjfkxxByZh(tcrzh, null, null);
            //处理特殊字段值
            Map<String, TYyxtCode> wtlxMap = this.getCodeMapCodeclass(CodeConstants.WTLX);
            Map<String, TYyxtCode> wtztMap = this.getCodeMapCodeclass(CodeConstants.WTZT);
            TYyxtCode tYyxtCode = null;
            ResultJson res1 = null;
            for (ViewYjfkxx yjfk : yjfkList) {
                tYyxtCode = wtlxMap.get(CodeConstants.WTLX + yjfk.getWtlx());
                if (tYyxtCode != null) {
                    yjfk.setWtlxDesc(tYyxtCode.getCodedesc());
                }
                tYyxtCode = wtztMap.get(CodeConstants.WTZT + yjfk.getWtzt());
                if (tYyxtCode != null) {
                    yjfk.setWtztDesc(tYyxtCode.getCodedesc());
                }
                yjfk.setTcsjFormat(DateUtils.format(yjfk.getTcsj(), DateUtils.PATTERN_DATETIME));
                if (yjfk.getFksj() != null && StringUtils.isNotBlank(yjfk.getFksj() + "")) {
                    yjfk.setFksjFormat(DateUtils.format(yjfk.getFksj(), DateUtils.PATTERN_DATETIME));
                }

                //根据附件路径-获取问题内容
                res1 = this.readTxtFromDFS(appConfig.getDfsHost() + "/" + yjfk.getWtnr());
                if (res1 != null && res1.isResult()) {
                    yjfk.setWtnr(res1.getObject().toString());
                }
                //根据附件路径-获取反馈内容
                res1 = null;
                res1 = this.readTxtFromDFS(appConfig.getDfsHost() + "/" + yjfk.getFknr());
                if (res1 != null && res1.isResult()) {
                    yjfk.setFknr(res1.getObject().toString());
                }
                //会员头像
                yjfk.setYhtx(appConfig.getDfsHost() + yhtx);
            }
            return yjfkList;
        } else {
            return null;
        }
    }

}
